package client.socket;

import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;

public class DemoClientSend extends Thread {
	private Socket clientSocket;
	private OutputStream os = null;
	private boolean isSend = true;

	public DemoClientSend(Socket clientSocket) {
		this.clientSocket = clientSocket;
	}

	@Override
	public void run() {
		String message = "ặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặặ"
				+ "ặặặặặặặặặặặặặặặặặặặ";
		int i = 0;
		while (isSend) {
			splitMessage(message, i);
		}
		super.run();
	}

	private boolean send(Object t) {
		byte[] bt = encode(t);
		if (bt == null) {
			return false;
		}
		return sendByte(os, bt);
	}

	private void splitMessage(String message, int i) {
		byte[] bt = message.getBytes();
		int leng = bt.length;
		send(leng);

		try {
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		ByteBuffer bb = ByteBuffer.allocate(leng);
		bb.put(bt);
		bb.position(0);
		int realRead = leng;
		while (realRead > 1500) {
			byte[] btTmp = new byte[1500];
			bb.get(btTmp);
			realRead -= 1500;
			send(btTmp);

			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		byte[] btTmp = new byte[realRead];
		bb.get(btTmp);
		send(btTmp);

	}

	private byte[] encode(Object t) {
		byte[] bt = null;
		if (t instanceof Integer) {
			bt = new byte[4];
			ByteBuffer bb = ByteBuffer.allocate(4).putInt((int) t);
			bb.position(0);
			bb.get(bt);
		} else if (t instanceof String) {
			bt = ((String) t).getBytes();
		} else if (t instanceof byte[]) {
			bt = (byte[]) t;
		}
		return bt;
	}

	private boolean sendByte(OutputStream os, byte[] bt) {
		try {
			if (os == null) {
				os = clientSocket.getOutputStream();
			}
			os.write(bt);
			os.flush();
		} catch (Exception e) {
			isSend = false;
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
